Merge "Made Title cache use MapCacheLRU"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Thu, 26 Dec 2013 22:24:14 +0000 (22:24 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Thu, 26 Dec 2013 22:24:14 +0000 (22:24 +0000)
1  2 
includes/Title.php
includes/cache/MapCacheLRU.php

diff --combined includes/Title.php
   * @internal documentation reviewed 15 Mar 2010
   */
  class Title {
-       /** @name Static cache variables */
-       // @{
-       static private $titleCache = array();
-       // @}
+       /** @var MapCacheLRU */
+       static private $titleCache = null;
  
        /**
         * Title::newFromText maintains a cache to avoid expensive re-normalization of
@@@ -79,8 -77,8 +77,8 @@@
        var $mTitleProtection;            ///< Cached value for getTitleProtection (create protection)
        # Don't change the following default, NS_MAIN is hardcoded in several
        # places.  See bug 696.
 +      # Zero except in {{transclusion}} tags
        var $mDefaultNamespace = NS_MAIN; // /< Namespace index when there is no namespace
 -                                                                        # Zero except in {{transclusion}} tags
        var $mWatched = null;             // /< Is $wgUser watching this page? null if unfilled, accessed through userIsWatching()
        var $mLength = -1;                // /< The page length, 0 for special pages
        var $mRedirect = null;            // /< Is the article at this title a redirect?
                        throw new MWException( 'Title::newFromText given an object' );
                }
  
+               $cache = self::getTitleCache();
                /**
                 * Wiki pages often contain multiple links to the same page.
                 * Title normalization and parsing can become expensive on
                 *
                 * In theory these are value objects and won't get changed...
                 */
-               if ( $defaultNamespace == NS_MAIN && isset( Title::$titleCache[$text] ) ) {
-                       return Title::$titleCache[$text];
+               if ( $defaultNamespace == NS_MAIN && $cache->has( $text ) ) {
+                       return $cache->get( $text );
                }
  
                # Convert things like &eacute; &#257; or &#x3017; into normalized (bug 14952) text
                $t->mDbkeyform = str_replace( ' ', '_', $filteredText );
                $t->mDefaultNamespace = $defaultNamespace;
  
-               static $cachedcount = 0;
                if ( $t->secureAndSplit() ) {
                        if ( $defaultNamespace == NS_MAIN ) {
-                               if ( $cachedcount >= self::CACHE_MAX ) {
-                                       # Avoid memory leaks on mass operations...
-                                       Title::$titleCache = array();
-                                       $cachedcount = 0;
-                               }
-                               $cachedcount++;
-                               Title::$titleCache[$text] =& $t;
+                               $cache->set( $text, $t );
                        }
                        return $t;
                } else {
                }
        }
  
+       /**
+        * @return MapCacheLRU
+        */
+       private static function getTitleCache() {
+               if ( self::$titleCache == null ) {
+                       self::$titleCache = new MapCacheLRU( self::CACHE_MAX );
+               }
+               return self::$titleCache;
+       }
        /**
         * Returns a list of fields that are to be selected for initializing Title objects or LinkCache entries.
         * Uses $wgContentHandlerUseDB to determine whether to include page_content_model.
         * @return String DB key
         */
        function getUserCaseDBKey() {
 -              return $this->mUserCaseDBKey;
 +              if ( !is_null( $this->mUserCaseDBKey ) ) {
 +                      return $this->mUserCaseDBKey;
 +              } else {
 +                      // If created via makeTitle(), $this->mUserCaseDBKey is not set.
 +                      return $this->mDbkeyform;
 +              }
        }
  
        /**
                $dbkey = preg_replace( '/[ _\xA0\x{1680}\x{180E}\x{2000}-\x{200A}\x{2028}\x{2029}\x{202F}\x{205F}\x{3000}]+/u', '_', $dbkey );
                $dbkey = trim( $dbkey, '_' );
  
 -              if ( $dbkey == '' ) {
 -                      return false;
 -              }
 -
                if ( strpos( $dbkey, UTF8_REPLACEMENT ) !== false ) {
                        # Contained illegal UTF-8 sequences or forbidden Unicode chars.
                        return false;
  
                # Initial colon indicates main namespace rather than specified default
                # but should not create invalid {ns,title} pairs such as {0,Project:Foo}
 -              if ( ':' == $dbkey[0] ) {
 +              if ( $dbkey !== '' && ':' == $dbkey[0] ) {
                        $this->mNamespace = NS_MAIN;
                        $dbkey = substr( $dbkey, 1 ); # remove the colon but continue processing
                        $dbkey = trim( $dbkey, '_' ); # remove any subsequent whitespace
                }
  
 +              if ( $dbkey == '' ) {
 +                      return false;
 +              }
 +
                # Namespace or interwiki prefix
                $firstPass = true;
                $prefixRegexp = "/^(.+?)_*:_*(.*)$/S";
         * Get all extant redirects to this Title
         *
         * @param int|Null $ns Single namespace to consider; NULL to consider all namespaces
 -       * @return Array of Title redirects to this title
 +       * @return Title[] Array of Title redirects to this title
         */
        public function getRedirectsHere( $ns = null ) {
                $redirs = array();
@@@ -28,7 -28,6 +28,7 @@@
   *
   * @see ProcessCacheLRU
   * @ingroup Cache
 + * @since 1.23
   */
  class MapCacheLRU {
        /** @var Array */
                $this->cache[$key] = $value;
        }
  
+       /**
+        * Check if a key exists
+        *
+        * @param $key string
+        * @return bool
+        */
+       public function has( $key ) {
+               return isset( $this->cache[$key] );
+       }
        /**
         * Get the value for a key.
         * This returns null if the key is not set.
         * If the item is already set, it will be pushed to the top of the cache.
         *
         * @param $key string
-        * @param $prop string
         * @return mixed
         */
        public function get( $key ) {